package org.anachronos.jeep.core.trace.impl;


import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElements;

public abstract class Invocation {
	@XmlElement
	private Metrics metrics = new Metrics();

	@XmlElements(value = {
			@XmlElement(name = "methodInvocation", type = MethodInvocation.class),
			@XmlElement(name = "sqlStatementExecution", type = SqlStatementExecution.class),
			@XmlElement(name = "transaction", type = Transaction.class) })
	private List<Invocation> children = new ArrayList<Invocation>();
	
	private transient Map<Object, Invocation> invocationLookup;
	
	public List<Invocation> getChildren() {
		return children;
	}

	/**
	 * Provides a filter version of this objects children
	 * 
	 * @return a unmodifiable collection containing all method invocation
	 *         children
	 */
	public List<MethodInvocation> getMethodInvocations() {
		final List<MethodInvocation> result = new ArrayList<MethodInvocation>();
		for (Invocation invocation : getChildren()) {
			if (invocation instanceof MethodInvocation) {
				MethodInvocation methodInvocation = (MethodInvocation) invocation;
				result.add(methodInvocation);
			}
		}
		return Collections.unmodifiableList(result);
	}

	/**
	 * Provides a filter version of this objects children
	 * 
	 * @return a unmodifiable collection containing all sql execution statement
	 *         children
	 */
	public List<SqlStatementExecution> getSqlStatementExecutions() {
		final List<SqlStatementExecution> result = new ArrayList<SqlStatementExecution>();
		for (Invocation invocation : getChildren()) {
			if (invocation instanceof SqlStatementExecution) {
				SqlStatementExecution sqlStatementExecution = (SqlStatementExecution) invocation;
				result.add(sqlStatementExecution);
			}
		}
		return Collections.unmodifiableList(result);
	}

	@SuppressWarnings("unchecked")
	public final <I extends Invocation> I lookupOrAdd(final I invocation) {
		if (invocationLookup == null) {
			initInvocationLookup();
		}
		if (!invocationLookup.containsKey(invocation.id())) {
			invocationLookup.put(invocation.id(), invocation);
			children.add(invocation);
			return invocation;
		} else {
			final I lookup = (I) invocationLookup.get(invocation.id());
			return lookup;
		}
	}
	
	public void add(Transaction transaction) {
		getChildren().add(transaction);
	}


	public abstract void accept(ModelVisitor visitor);
	
	public abstract Object id();
	
	public Metrics getMetrics() {
		return metrics;
	}

	public abstract Invocation shallowCopy();
	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((children == null) ? 0 : children.hashCode());
		result = prime * result + ((metrics == null) ? 0 : metrics.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Invocation other = (Invocation) obj;
		if (children == null) {
			if (other.children != null)
				return false;
		} else if (!children.equals(other.children))
			return false;
		if (metrics == null) {
			if (other.metrics != null)
				return false;
		} else if (!metrics.equals(other.metrics))
			return false;
		return true;
	}
	
	private void initInvocationLookup() {
		invocationLookup = new HashMap<Object, Invocation>();
		for (final Invocation invocation : getChildren()) {
			invocationLookup.put(invocation.id(), invocation);
		}
	}
}
